package com.jerry.lastgomall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jerry.common.utils.PageUtils;
import com.jerry.common.utils.Query;
import com.jerry.lastgomall.product.dao.PmsProductDao;
import com.jerry.lastgomall.product.entity.PmsProductEntity;
import com.jerry.lastgomall.product.service.PmsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service("pmsProductService")
public class PmsProductServiceImpl extends ServiceImpl<PmsProductDao, PmsProductEntity> implements PmsProductService {

    @Autowired
    private RedisTemplate redisTemplate;

    //todo 堆外内存溢出
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<PmsProductEntity> productList = (IPage<PmsProductEntity>) redisTemplate.opsForValue().get("productList");

        if (productList==null){
            //缓存中没有,去数据库查询
            IPage<PmsProductEntity> page = this.page(
                    new Query<PmsProductEntity>().getPage(params),
                    new QueryWrapper<PmsProductEntity>()
            );
            redisTemplate.opsForValue().set("productList",page,1, TimeUnit.DAYS);
            //数据库存放JSON格式

            System.out.println("数据库查询");
            return new PageUtils(page);
        }
        System.out.println("缓存查询");
        return new PageUtils(productList);
    }

}